package winOrDie.Configuration;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * This public class provudes {@link WoD_Configuration} XML editor 
 *
 */
public class WoD_Configuration_Editor 
{
	/**
	 * {@link WoD_Configuration_Editor} XML Elements and attributes
	 */
	public static class XML_ELEMENTS_WoD_Configuration
	{
		public static String Element_WoD_Configuration = "WoD_Configuration";
		public static String Element_Communication_Ports = "COM_Ports";
		public static String Attribute_Control_Port = "Control_Port";
		public static String Attribute_RT_Port = "RT_Port";
		public static String Attribute_Voice_Port = "Voice_Port";
		public static String Element_Servers = "Servers";
		public static String Attribute_Web_Server = "Web_Server";
		public static String Attribute_Web_Server_Port = "Web_Server_Port";
		public static String Attribute_Proxy_Server = "Proxy_Server";
	}

	
	/**
	 * This public static method returns {@link Element} from {@link WoD_Configuration} and {@link Document} passed as parameters
	 * @param woDConfiguration
	 * @param document
	 * @return {@link Element}
	 */
	public static Element get_Element_From_Configuration(
			WoD_Configuration woDConfiguration,
			Document document
			)
	{
		Element element_WoD_Configuration = document.createElement(XML_ELEMENTS_WoD_Configuration.Element_WoD_Configuration);
		
		
		/**
		 * Communication ports
		 */
		Element element_Communications_Ports = document.createElement(XML_ELEMENTS_WoD_Configuration.Element_Communication_Ports);
		element_Communications_Ports.setAttribute(
				XML_ELEMENTS_WoD_Configuration.Attribute_Control_Port,
				String.valueOf(woDConfiguration.getControl_Port())
				);
		element_Communications_Ports.setAttribute(
				XML_ELEMENTS_WoD_Configuration.Attribute_RT_Port,
				String.valueOf(woDConfiguration.getRT_Port())
				);
		element_Communications_Ports.setAttribute(
				XML_ELEMENTS_WoD_Configuration.Attribute_Voice_Port,
				String.valueOf(woDConfiguration.getVoice_Port())
				);
		element_WoD_Configuration.appendChild(element_Communications_Ports);
		
		/**
		 * Servers
		 */
		Element element_Servers = document.createElement(XML_ELEMENTS_WoD_Configuration.Element_Servers);
		element_Servers.setAttribute(
				XML_ELEMENTS_WoD_Configuration.Attribute_Web_Server, 
				woDConfiguration.getWeb_Server()
				);
		element_Servers.setAttribute(
				XML_ELEMENTS_WoD_Configuration.Attribute_Web_Server_Port, 
				woDConfiguration.getWeb_Server_Port()
				);
		element_Servers.setAttribute(
				XML_ELEMENTS_WoD_Configuration.Attribute_Proxy_Server, 
				woDConfiguration.getProxy_Server()
				);
		element_WoD_Configuration.appendChild(element_Servers);
		
		return element_WoD_Configuration;
		
	}
	
	
	
	/**
	 * This public static method returns {@link WoD_Configuration} object from {@link Element} passed as parameter
	 * @param element
	 * @return {@link WoD_Configuration}
	 * @throws Exception
	 */
	public static WoD_Configuration get_WoD_Configuration_From_Element(Element element) throws Exception
	{
		if (!element.getNodeName().equals(XML_ELEMENTS_WoD_Configuration.Element_WoD_Configuration))
		{
			Exception exception = new Exception("The element is not a WoD_Configuration element.");
			throw exception;
		}
		
		WoD_Configuration woDConfiguration  = new WoD_Configuration();
		
		
		/**
		 * Communication ports
		 */
		NodeList nodeList = element.getElementsByTagName(XML_ELEMENTS_WoD_Configuration.Element_Communication_Ports);
		if (nodeList.getLength() < 1)
		{
			Exception exception = new Exception("The element is not a WoD_Configuration element. Item " + 
					XML_ELEMENTS_WoD_Configuration.Element_Communication_Ports + " not found.");
			throw exception;
		}
		
		Element element_Communication_Ports = (Element)nodeList.item(0);
		
		woDConfiguration.setControl_Port(
				Integer.valueOf(
						element_Communication_Ports.getAttribute(XML_ELEMENTS_WoD_Configuration.Attribute_Control_Port)
				)
			);
		
		woDConfiguration.setRT_Port(
				Integer.valueOf(
						element_Communication_Ports.getAttribute(XML_ELEMENTS_WoD_Configuration.Attribute_RT_Port)
				)
			);
		
		woDConfiguration.setVoice_Port(
				Integer.valueOf(
						element_Communication_Ports.getAttribute(XML_ELEMENTS_WoD_Configuration.Attribute_Voice_Port)
				)
			);
		
		
		
		/**
		 * Servers
		 */
		nodeList = element.getElementsByTagName(XML_ELEMENTS_WoD_Configuration.Element_Servers);
		if (nodeList.getLength() < 1)
		{
			Exception exception = new Exception("The element is not a WoD_Configuration element. Item " + 
					XML_ELEMENTS_WoD_Configuration.Element_Servers + " not found.");
			throw exception;
		}
		
		Element element_Servers = (Element)nodeList.item(0);
		
		woDConfiguration.setWeb_Server(
				element_Servers.getAttribute(XML_ELEMENTS_WoD_Configuration.Attribute_Web_Server)
				);
		
		woDConfiguration.setWeb_Server_Port(
				element_Servers.getAttribute(XML_ELEMENTS_WoD_Configuration.Attribute_Web_Server_Port)
				);
		
		woDConfiguration.setProxy_Server(
				element_Servers.getAttribute(XML_ELEMENTS_WoD_Configuration.Attribute_Proxy_Server)
				);
				
		return woDConfiguration;
	}
}
